// 组合两数只和 40
// candidates = [10,1,2,7,6,1,5], target = 8, 总数相加为8
function candidate(arr, target) {
    let len = arr.length;
    let result = [];
    let temp = [];
    arr.sort((a, b) => a - b)
    function back(temp, target, start) {
        if (target == 0) {
            result.push(temp)
            return
        }
        for (let i = start; i < len; i++) {
            if (target < arr[i]) break // 避免无线循环
            // 同层不做处理
            if (i > start && arr[i - 1] == arr[i]) continue
            temp.push(arr[i])
            back(temp.slice(), target - arr[i], i+1) // +1 不能用重复元素
            temp.pop() // 这就是回溯
        }

    }
    back(temp, target, 0)
    return result

}
console.log("求和", candidate([10, 1, 4, 4, 2, 7, 6, 1, 5], 8))



// https://pic.leetcode-cn.com/1625367960-UqkYXa-file_1625367957583